package me.firstapp.bbs.controller.interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonMember;
import me.firstapp.common.module.bbs.JsonPointAccount;
import me.firstapp.common.utils.CookieUtils;
import me.firstapp.common.utils.SpringContextTool;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.spi.bbs.CollectionService;
import me.firstapp.spi.bbs.TopicService;
import me.firstapp.spi.shop.PointService;

public class OtherInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception e)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		// 判断登录
		Cookie cookie = CookieUtils.getCookieByName(request, "loginToken");
		if(cookie != null){
			String loginToken = cookie.getValue();
			if (StrUtils.isNotNULL(loginToken)) {
				ApplicationContext applicationContext = SpringContextTool.getApplicationContext();
				@SuppressWarnings("unchecked")
				RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) applicationContext.getBean("redisTemplate");
				JsonMember member = (JsonMember) redisTemplate.opsForValue().get("login_member_" + loginToken);
				if (member != null) {
					request.setAttribute("member", member);
					// 积分余额
					PointService apiPointService = SpringContextTool.getApplicationContext().getBean(PointService.class);
					SingleObject<JsonPointAccount> accountResult = apiPointService.findPointAccount(member.getId());
					JsonPointAccount pointAccount = accountResult.getObject();
					request.setAttribute("points", pointAccount == null ? 0 : pointAccount.getCurrentValue());

					// 话题数
					TopicService apiTopicService = SpringContextTool.getApplicationContext().getBean(TopicService.class);
					SingleObject<Long> topicCountsResult = apiTopicService.findMemberTopicsCounts(member.getId());
					Long topicCounts = topicCountsResult.getObject();
					request.setAttribute("topicCounts", topicCounts == null ? 0 : topicCounts);

					// 收藏数
					CollectionService apiCollectionService = SpringContextTool.getApplicationContext()
							.getBean(CollectionService.class);
					SingleObject<Long> colCountsResult = apiCollectionService.findCollectionCounts(member.getId(), null,
							null);
					Long colCounts = colCountsResult.getObject();
					request.setAttribute("colCounts", colCounts == null ? 0 : colCounts);
				}
			}
		}

		return true;
	}

}
